import asyncio
import ygg_aiomysql as aiomysql

async def main():
    """
    主函数: 演示数据删除完整流程
    """
    # 创建数据库连接池
    pool = await aiomysql.new_pool_env()
    
    try:
        # 创建测试表
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("""
                    CREATE TABLE IF NOT EXISTS test_table (
                        id INT AUTO_INCREMENT PRIMARY KEY,
                        name VARCHAR(100),
                        age INT
                    )
                """)
                await conn.commit()
        
        # 先插入测试数据
        test_data = {'name': '待删除用户', 'age': 25}
        inserted_id = await aiomysql.add(pool, 'test_table', test_data)
        print(f"初始数据插入成功，ID: {inserted_id}")
        
        # 调用delete方法删除数据
        print(f"准备删除ID为{inserted_id}的数据")
        affected_rows = await aiomysql.delete(pool, 'test_table', inserted_id)
        print(f"数据删除成功，影响行数: {affected_rows}")
        
        # 验证数据删除结果
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("SELECT * FROM test_table WHERE id = %s", (inserted_id,))
                result = await cur.fetchone()
                print(f"删除后查询结果: {'数据已删除' if result is None else '数据仍存在'}")
                
    finally:
        # 清理测试表
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("DROP TABLE IF EXISTS test_table")
                await conn.commit()
        
        # 关闭连接池
        pool.close()
        await pool.wait_closed()

if __name__ == '__main__':
    asyncio.run(main())